package com.example.newhabitclock.TimeAdd


import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.newhabitclock.databinding.ItemMarkBinding

class MarkAdapter : RecyclerView.Adapter<MarkAdapter.MarkViewHolder>() {
    private var marks: List<Mark> = emptyList()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MarkViewHolder {
        val binding = ItemMarkBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return MarkViewHolder(binding)
    }

    override fun onBindViewHolder(holder: MarkViewHolder, position: Int) {
        holder.bind(marks[position])
    }

    override fun getItemCount(): Int = marks.size

    fun submitList(newMarks: List<Mark>) {
        marks = newMarks
        notifyDataSetChanged() // For simplicity; consider DiffUtil for production
    }

    inner class MarkViewHolder(private val binding: ItemMarkBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(mark: Mark) {
            binding.markNumberText.text = String.format("%02d", mark.number)
            binding.timeDifferenceText.text = "+ ${formatTime(mark.timeDifference)}"
            binding.absoluteTimeText.text = formatTime(mark.absoluteTime)
        }

        private fun formatTime(millis: Long): String {
            val minutes = (millis / 60000).toInt()
            val seconds = ((millis / 1000) % 60).toInt()
            val hundredths = ((millis / 10) % 100).toInt()
            return String.format("%02d:%02d.%02d", minutes, seconds, hundredths)
        }
    }
}